﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PivotServerTools.Internal
{

    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Net;
    using Microsoft.SharePoint;

    /// <summary>    
    /// Create an item image by loading it from a SharePoint document library URL.    
    /// </summary>
    internal class SPImage : ImageBase
    {
        // Private Fields
        //======================================================================

        Uri m_url;
        byte[] m_imageData;

        // Constructors, Finalizer and Dispose
        //======================================================================

        public SPImage(Uri url)
        {
            m_url = url;
        }

        // Properties
        #region Properties

        public override byte[] ImageData
        {
            get
            {
                EnsureIsLoaded();
                return this.m_imageData;
            }
        }
        protected new byte[] ImageDataInternal
        {
            get { return this.m_imageData; }
        }


        #endregion
        // Protected Methods
        //======================================================================

        protected override Image MakeImage()
        {
            string s_url = m_url.ToString();
            using (SPSite site = new SPSite(s_url))
            {
                using (SPWeb web = site.RootWeb)
                {
                    object o = web.GetFileOrFolderObject(s_url);
                    if (o is SPFile)
                    {
                        SPFile f = (SPFile)o;
                        using (Stream ImageStream = f.OpenBinaryStream(SPOpenBinaryOptions.SkipVirusScan))
                        {
                            int imageLength = (int)ImageStream.Length;
                            Image LoadedImage = Image.FromStream(ImageStream);                      
                                using (MemoryStream stream = new MemoryStream())
                                {
                                    LoadedImage.Save(stream, ImageFormat.Jpeg);
                                    m_imageData = stream.ToArray();
                                }
                            
                            return LoadedImage;
                        }

                    }
                    throw new FileNotFoundException();
                }
            }

        }


        protected override void EnsureIsLoaded()
        {
            //Note, this method can be called concurrently by multiple image-request threads.
            // TODO: For non-sample use, lock the portion of code that loads the image to avoid
            // duplicating work, or implement a shared queue that manages asynchronous loading of all source images.

            if (null == m_imageData)
            {                
                    using (Image image = MakeImage())
                    {
                        this.Size = image.Size;
                    }                
            }
        }
    }
}
